package model

import "gorm.io/gorm"

type FileCondition struct {
	ID           string `json:"id"`
	OriginalName string `json:"original_name"`
	DisplayName  string `json:"display_name"`
	FileType     string `json:"file_type"`
	CreatedAt    string `json:"created_at"`
}

type FileClient struct {
	db *gorm.DB
}

func NewFileClient(db *gorm.DB) *FileClient {
	return &FileClient{db: db}
}

// 上传文件
func (c *FileClient) UploadFile(file *File) error {
	return c.db.Create(file).Error
}

// 查询文件
func (c *FileClient) FindFileByCondition(condition *FileCondition) (*File, error) {
	// 构建查询
	query := c.db.Model(&File{})
	if condition.ID != "" {
		query = query.Where("id = ?", condition.ID)
	}
	if condition.OriginalName != "" {
		query = query.Where("original_name LIKE ?", "%"+condition.OriginalName+"%")
	}
	if condition.DisplayName != "" {
		query = query.Where("display_name LIKE ?", "%"+condition.DisplayName+"%")
	}
	if condition.FileType != "" {
		query = query.Where("file_type = ?", condition.FileType)
	}
	if condition.CreatedAt != "" {
		query = query.Where("created_at >= ?", condition.CreatedAt)
	}

	// 执行查询
	var file *File
	if err := query.First(&file).Error; err != nil {
		return nil, err
	}

	return file, nil
}
